package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.ruoyi.system.domain.PoPublicOpinion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OpinionForecastMapper;
import com.ruoyi.system.domain.OpinionForecast;
import com.ruoyi.system.service.IOpinionForecastService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-28
 */
@Service
public class OpinionForecastServiceImpl implements IOpinionForecastService 
{
    @Autowired
    private OpinionForecastMapper opinionForecastMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param opinionId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public OpinionForecast selectOpinionForecastById(Long opinionId)
    {
        return opinionForecastMapper.selectOpinionForecastById(opinionId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param opinionForecast 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<OpinionForecast> selectOpinionForecastList(OpinionForecast opinionForecast)
    {
        return opinionForecastMapper.selectOpinionForecastList(opinionForecast);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param opinionForecast 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertOpinionForecast(OpinionForecast opinionForecast)
    {
        return opinionForecastMapper.insertOpinionForecast(opinionForecast);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param opinionForecast 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateOpinionForecast(OpinionForecast opinionForecast)
    {
        return opinionForecastMapper.updateOpinionForecast(opinionForecast);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param opinionIds 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteOpinionForecastByIds(Long[] opinionIds)
    {
        return opinionForecastMapper.deleteOpinionForecastByIds(opinionIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param opinionId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteOpinionForecastById(Long opinionId)
    {
        return opinionForecastMapper.deleteOpinionForecastById(opinionId);
    }

    @Override
    public int batchInsertOpinionForecast(List<PoPublicOpinion> opinions, String warningLevel, String tenantCode, Long ruleId) {
        //去除已经插入的
        // 使用迭代器遍历列表并删除符合条件的元素
        Iterator<PoPublicOpinion> iterator = opinions.iterator();
        while (iterator.hasNext()) {
            PoPublicOpinion opinion = iterator.next();
            OpinionForecast forecast=new OpinionForecast();
            forecast.setOpinionId(opinion.getId());
            forecast.setLevel(warningLevel);
            forecast.setRuleId(ruleId);
            List<OpinionForecast> forecasts=  opinionForecastMapper.selectOpinionForecastList(forecast);
            if(forecasts!=null&&forecasts.size()>0){
                iterator.remove();
            }
        }
        List<OpinionForecast> forecastList=new ArrayList<>();
        for (PoPublicOpinion opinion: opinions) {
            OpinionForecast fore=new OpinionForecast();
            fore.setOpinionId(opinion.getId());
            fore.setLevel(warningLevel);
            fore.setTenantCode(tenantCode);
            fore.setRuleId(ruleId);
            forecastList.add(fore);
        }

        return  forecastList.size()>0?opinionForecastMapper.batchInsertOpinionForecast(forecastList):0;
    }
}
